<template>
  <div class="app-card" :class="{ featured: featured }">
    <div class="app-preview">
      <!-- 优先显示实际封面图片 -->
      <img
        v-if="app.appIcon"
        :src="app.appIcon"
        :alt="app.appName"
        class="app-image"
        @error="handleImageError"
      />
      <!-- 默认封面占位图 -->
      <div v-else class="app-placeholder">
        <div class="placeholder-content">
          <span class="placeholder-icon">📱</span>
          <span class="placeholder-text">{{ app.appName || '应用预览' }}</span>
        </div>
      </div>
      <div class="app-overlay">
        <a-button
          type="primary"
          class="view-chat-btn"
          @click="$emit('view-chat', app)"
        >
          查看对话
        </a-button>
        <a-button
          type="default"
          class="view-work-btn"
          @click="$emit('view-work', app)"
        >
          查看作品
        </a-button>
      </div>
    </div>
    <div class="app-info">
      <div class="app-meta">
        <!-- 优先显示实际头像 -->
        <img
          v-if="app.user?.userAvatar"
          :src="app.user.userAvatar"
          :alt="app.user.userName"
          class="user-avatar"
          @error="handleAvatarError"
        />
        <!-- 默认头像占位图 -->
        <div v-else class="user-avatar-placeholder">
          <span class="avatar-icon">👤</span>
        </div>
        <div class="app-details">
          <h3 class="app-title">{{ app.appName || '未命名应用' }}</h3>
          <p class="app-subtitle">{{ app.appType || '智能应用' }}</p>
          <p class="app-author">by {{ app.user?.userName || '匿名用户' }}</p>
        </div>
      </div>
      <p class="app-description">{{ app.appDesc || '这是一个精心设计的应用，为您提供优质的用户体验。' }}</p>
      <div class="app-footer">
        <div class="app-tags">
          <span class="creation-tag">使用NoCode创建</span>
        </div>
        <div class="app-stats">
          <span class="stat-item">{{ formatDate(app.createTime) }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

interface Props {
  app: API.AppVO
  featured?: boolean
  isDeployed?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  featured: false,
  isDeployed: false
})

const emit = defineEmits<{
  viewChat: [appId: string | number | undefined]
  viewWork: [app: API.AppVO]
}>()

// 获取用户头像，优先使用真实头像
const userAvatar = computed(() => {
  return props.app.user?.userAvatar || ''
})

// 获取用户名，优先使用真实用户名
const userName = computed(() => {
  return props.app.user?.userName || props.app.user?.userAccount || '匿名用户'
})

// 获取应用封面，优先使用真实封面
const appCover = computed(() => {
  return props.app.cover || ''
})

// 格式化创建时间
const formatTime = (timeStr: string | undefined) => {
  if (!timeStr) return ''
  const date = new Date(timeStr)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'short',
    day: 'numeric'
  })
}

const handleViewChat = () => {
  emit('viewChat', props.app.id)
}

const handleViewWork = () => {
  emit('viewWork', props.app)
}

// 处理图片加载错误
const handleImageError = (event: Event) => {
  const target = event.target as HTMLImageElement
  target.style.display = 'none'
}

// 处理头像加载错误
const handleAvatarError = (event: Event) => {
  const target = event.target as HTMLImageElement
  target.style.display = 'none'
}

// 格式化日期
const formatDate = (dateString: string) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'short',
    day: 'numeric'
  })
}
</script>

<style scoped>
.app-card {
  background: #ffffff;
  border-radius: 16px;
  overflow: hidden;
  box-shadow:
    0 4px 16px rgba(0, 0, 0, 0.08),
    0 2px 8px rgba(0, 0, 0, 0.04);
  border: 1px solid rgba(0, 0, 0, 0.06);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
}

.app-card.featured {
  border: 1px solid rgba(48, 183, 126, 0.2);
  box-shadow:
    0 6px 24px rgba(48, 183, 126, 0.12),
    0 3px 12px rgba(123, 211, 192, 0.08);
}

.app-card:hover {
  transform: translateY(-8px);
  box-shadow:
    0 12px 40px rgba(0, 0, 0, 0.15),
    0 6px 20px rgba(0, 0, 0, 0.08);
}

.app-card.featured:hover {
  box-shadow:
    0 16px 48px rgba(48, 183, 126, 0.2),
    0 8px 24px rgba(123, 211, 192, 0.12);
}

.app-preview {
  position: relative;
  height: 200px;
  overflow: hidden;
  background: #f8fafc;
}

.app-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.app-card:hover .app-image {
  transform: scale(1.05);
}

/* 优化默认封面占位图 */
.app-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
  color: #64748b;
}

.placeholder-content {
  text-align: center;
}

.placeholder-icon {
  font-size: 48px;
  display: block;
  margin-bottom: 8px;
  opacity: 0.6;
}

.placeholder-text {
  font-size: 14px;
  font-weight: 500;
  opacity: 0.8;
}

.app-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(48, 183, 126, 0.9) 0%, rgba(123, 211, 192, 0.8) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  opacity: 0;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  backdrop-filter: blur(10px);
}

.app-card:hover .app-overlay {
  opacity: 1;
}

/* 优化查看对话按钮 - 青草浅绿色渐变 */
.view-chat-btn {
  background: linear-gradient(135deg, #7dd3c0 0%, #5bc9a8 30%, #4bc0a0 70%, #3bb890 100%);
  border: none;
  border-radius: 24px;
  padding: 8px 20px;
  color: white;
  font-weight: 600;
  font-size: 14px;
  box-shadow: 0 4px 16px rgba(75, 192, 160, 0.3);
  transition: all 0.3s ease;
}

.view-chat-btn:hover {
  transform: translateY(-2px) scale(1.05);
  background: linear-gradient(135deg, #8de0cc 0%, #6bd1b4 30%, #5bc9a8 70%, #4bc0a0 100%);
  box-shadow: 0 6px 24px rgba(75, 192, 160, 0.4);
}

.view-work-btn {
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(75, 192, 160, 0.3);
  border-radius: 24px;
  padding: 8px 20px;
  color: #4bc0a0;
  font-weight: 600;
  font-size: 14px;
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.view-work-btn:hover {
  background: rgba(255, 255, 255, 1);
  transform: translateY(-2px) scale(1.05);
  box-shadow: 0 6px 20px rgba(75, 192, 160, 0.2);
}

.app-info {
  padding: 20px;
}

.app-meta {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 16px;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #f1f5f9;
  flex-shrink: 0;
}

/* 优化默认头像占位图 */
.user-avatar-placeholder {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #e2e8f0 0%, #cbd5e1 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #f1f5f9;
  flex-shrink: 0;
}

.avatar-icon {
  font-size: 18px;
  opacity: 0.6;
}

.app-details {
  flex: 1;
  min-width: 0;
}

.app-title {
  font-size: 18px;
  font-weight: 700;
  margin: 0 0 4px;
  color: #1e293b;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.app-subtitle {
  font-size: 13px;
  color: #4bc0a0;
  margin: 0 0 4px;
  font-weight: 500;
}

.app-author {
  font-size: 12px;
  color: #64748b;
  margin: 0;
}

.app-description {
  font-size: 14px;
  color: #475569;
  line-height: 1.5;
  margin: 0 0 16px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.app-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.app-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.creation-tag {
  background: linear-gradient(135deg, rgba(48, 183, 126, 0.1) 0%, rgba(123, 211, 192, 0.08) 100%);
  color: #30b77e;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  border: 1px solid rgba(48, 183, 126, 0.2);
}

.app-stats {
  display: flex;
  gap: 12px;
}

.stat-item {
  font-size: 11px;
  color: #94a3b8;
  font-weight: 500;
}

/* 精选案例特殊样式 */
.app-card.featured .app-title {
  background: linear-gradient(135deg, #30b77e 0%, #4bc0a0 50%, #7dd3c0 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* 部署状态样式 */
.deploy-status {
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 10px;
  font-weight: 500;
}

.deploy-status.deployed {
  background: rgba(34, 197, 94, 0.1);
  color: #22c55e;
  border: 1px solid rgba(34, 197, 94, 0.2);
}

.deploy-status.not-deployed {
  background: rgba(156, 163, 175, 0.1);
  color: #9ca3af;
  border: 1px solid rgba(156, 163, 175, 0.2);
}

/* 禁用状态的查看作品按钮 */
.view-work-btn.disabled {
  background: rgba(156, 163, 175, 0.1) !important;
  color: #9ca3af !important;
  cursor: not-allowed !important;
  border: 1px solid rgba(156, 163, 175, 0.2) !important;
}

.view-work-btn.disabled:hover {
  transform: none !important;
  box-shadow: none !important;
  background: rgba(156, 163, 175, 0.1) !important;
}

.view-work-btn.disabled::before {
  display: none !important;
}
</style>
